{% extends 'base.html' %}
{% load static %}

{% block title %}标签打印历史{% endblock %}

{% block extra_css %}
<link href="{% static 'css/label-ui.css' %}" rel="stylesheet">
{% endblock %}

{% block content %}
<div class="container-fluid">
    <!-- 面包屑导航 -->
    <nav aria-label="breadcrumb" class="mb-4">
        <ol class="breadcrumb">
            <li class="breadcrumb-item"><a href="{% url 'assets:index' %}">首页</a></li>
            <li class="breadcrumb-item"><a href="{% url 'assets:asset_list' %}">资产管理</a></li>
            <li class="breadcrumb-item active">标签打印历史</li>
        </ol>
    </nav>

    <div class="row">
        <div class="col-md-8">
            <!-- 搜索和筛选 -->
            <div class="search-card">
                <div class="row align-items-center">
                    <div class="col-md-8">
                        <form method="get" class="d-flex">
                            <input type="text" class="form-control me-2" name="search" 
                                   value="{{ search_query }}" placeholder="搜索资产编号或文件名...">
                            <button type="submit" class="btn btn-primary">
                                <i class="fas fa-search"></i>
                            </button>
                        </form>
                    </div>
                    <div class="col-md-4 text-end">
                        <a href="{% url 'assets:asset_batch_print_labels' %}" class="btn btn-success">
                            <i class="fas fa-print me-2"></i>打印标签
                        </a>
                    </div>
                </div>
            </div>
            
            <!-- 打印历史列表 -->
            {% if page_obj %}
                {% for history in page_obj %}
                    {% include 'assets/partials/history_item.html' with history=history %}
                {% endfor %}
                
                <!-- 分页 -->
                {% if page_obj.has_other_pages %}
                    <nav aria-label="历史记录分页" class="mt-4">
                        <ul class="pagination justify-content-center">
                            {% if page_obj.has_previous %}
                                <li class="page-item">
                                    <a class="page-link" href="?page=1{% if search_query %}&search={{ search_query }}{% endif %}">首页</a>
                                </li>
                                <li class="page-item">
                                    <a class="page-link" href="?page={{ page_obj.previous_page_number }}{% if search_query %}&search={{ search_query }}{% endif %}">上一页</a>
                                </li>
                            {% endif %}
                            
                            <li class="page-item active">
                                <span class="page-link">{{ page_obj.number }} / {{ page_obj.paginator.num_pages }}</span>
                            </li>
                            
                            {% if page_obj.has_next %}
                                <li class="page-item">
                                    <a class="page-link" href="?page={{ page_obj.next_page_number }}{% if search_query %}&search={{ search_query }}{% endif %}">下一页</a>
                                </li>
                                <li class="page-item">
                                    <a class="page-link" href="?page={{ page_obj.paginator.num_pages }}{% if search_query %}&search={{ search_query }}{% endif %}">末页</a>
                                </li>
                            {% endif %}
                        </ul>
                    </nav>
                {% endif %}
            {% else %}
                <div class="text-center py-5">
                    <i class="fas fa-history fa-3x text-muted mb-3"></i>
                    <h5 class="text-muted">暂无打印历史</h5>
                    <p class="text-muted">开始打印标签后，历史记录将显示在这里</p>
                    <a href="{% url 'assets:asset_batch_print_labels' %}" class="btn btn-primary">
                        <i class="fas fa-print me-2"></i>开始打印标签
                    </a>
                </div>
            {% endif %}
        </div>
        
        <div class="col-md-4">
            <!-- 统计信息 -->
            <div class="stats-card">
                <h5 class="mb-3">
                    <i class="fas fa-chart-bar me-2"></i>
                    打印统计
                </h5>
                
                <div class="stat-item">
                    <span class="stat-number">{{ page_obj.paginator.count|default:0 }}</span>
                    <span class="stat-label">总打印次数</span>
                </div>
                
                <div class="stat-item">
                    <span class="stat-number">
                        {% widthratio page_obj.paginator.count 1 1 as total_assets %}
                        {{ total_assets|default:0 }}
                    </span>
                    <span class="stat-label">累计打印资产</span>
                </div>
            </div>
            
            <!-- 快捷操作 -->
            <div class="search-card">
                <h6 class="mb-3">
                    <i class="fas fa-bolt text-warning me-2"></i>
                    快捷操作
                </h6>
                
                <div class="d-grid gap-2">
                    <a href="{% url 'assets:asset_batch_print_labels' %}" class="btn btn-outline-primary">
                        <i class="fas fa-print me-2"></i>批量打印标签
                    </a>
                    <a href="{% url 'assets:label_template_list' %}" class="btn btn-outline-info">
                        <i class="fas fa-template me-2"></i>管理标签模板
                    </a>
                    <a href="{% url 'assets:asset_list' %}" class="btn btn-outline-success">
                        <i class="fas fa-list me-2"></i>资产列表
                    </a>
                </div>
            </div>
            
            <!-- 使用提示 -->
            <div class="search-card">
                <h6 class="mb-3">
                    <i class="fas fa-info-circle text-info me-2"></i>
                    使用提示
                </h6>
                
                <ul class="list-unstyled small">
                    <li class="mb-2">
                        <i class="fas fa-check text-success me-2"></i>
                        历史记录保存所有打印配置
                    </li>
                    <li class="mb-2">
                        <i class="fas fa-check text-success me-2"></i>
                        可以查看每次打印的资产列表
                    </li>
                    <li class="mb-2">
                        <i class="fas fa-check text-success me-2"></i>
                        支持按资产编号搜索历史
                    </li>
                    <li class="mb-2">
                        <i class="fas fa-check text-success me-2"></i>
                        记录打印时间和操作人员
                    </li>
                </ul>
            </div>
        </div>
    </div>
</div>
{% endblock %}

{% block extra_js %}
<script>
document.addEventListener('DOMContentLoaded', function() {
    // 搜索表单自动提交
    const searchInput = document.querySelector('input[name="search"]');
    if (searchInput) {
        let timeout;
        searchInput.addEventListener('input', function() {
            clearTimeout(timeout);
            timeout = setTimeout(() => {
                this.form.submit();
            }, 500);
        });
    }
});
</script>
{% endblock %}